import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:hong_vpn_new_20240626/utils/configs/color_config.dart';
import 'package:hong_vpn_new_20240626/utils/configs/text_style_config.dart';
import 'package:hong_vpn_new_20240626/utils/global.dart';
import 'package:hong_vpn_new_20240626/utils/models/chat.dart';

class ChatCellWidget extends StatefulWidget {
  final ChatModel chatModel;

  const ChatCellWidget({super.key, required this.chatModel});

  @override
  State<ChatCellWidget> createState() => _ChatCellWidgetState();
}

class _ChatCellWidgetState extends State<ChatCellWidget> {
  late bool isAnswer = false;

  @override
  void initState() {
    super.initState();
    isAnswer = widget.chatModel.role == UserRole.assistant ||
        widget.chatModel.role == UserRole.loading ||
        widget.chatModel.role == UserRole.error ||
        widget.chatModel.role == UserRole.welcome;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(15).copyWith(bottom: 0),
      alignment: isAnswer ? Alignment.centerLeft : Alignment.centerRight,
      child: Container(
          padding: const EdgeInsets.all(10),
          constraints:
              BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.8),
          decoration: BoxDecoration(
              color: ColorConfig.textWhite.withOpacity(0.1),
              borderRadius: const BorderRadius.all(Radius.circular(15))),
          child: switch (widget.chatModel.role) {
            UserRole.loading => loading(),
            UserRole.error => error(),
            UserRole.welcome => welcome(),
            _ => normalMessage(),
          }),
    );
  }

  Widget loading() {
    return const CupertinoActivityIndicator(
      color: Colors.white,
      radius: 12,
    );
  }

  Widget welcome() {
    return Text(
      widget.chatModel.content,
      // style: TextStyleConfig.value,
    );
  }

  Widget error() {
    return Text(
      widget.chatModel.content,
      // style: TextStyleConfig.value,
    );
  }

  Widget normalMessage() {
    return Column(
      crossAxisAlignment:
          isAnswer ? CrossAxisAlignment.end : CrossAxisAlignment.start,
      children: [
        // Text(widget.chatModel.content, style: TextStyleConfig.middle),
        MarkdownBody(data: widget.chatModel.content),
        Text(
          widget.chatModel.time ?? "",
          style: TextStyleConfig.value
              .size(10)
              .copyWith(color: Colors.white.withOpacity(0.5)),
        ),
      ],
    );
  }
}
